<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- ref属性可以用于获取dom元素 -->
      <!-- 获取的时机必须在mounted之后 -->
      <h2 ref="h2dom">特殊属性ref</h2>
      <button @click="fn">btn</button>

      <hr />

      <!-- 如果ref给组件加，获取的是子组件实例 -->
      <!-- 可以获取到子组件的属性和方法了 -->
      <!-- 可以做到子传父了 -->
      <child ref="child"></child>
      <button @click="fn2">btn2</button>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const { createApp } = Vue;

      const Child = {
        data() {
          return {
            name: "zhangsan",
          };
        },
        methods: {
          log() {
            console.log("child log");
          },
        },
        template: `
          <div>child组件</div>
        `,
      };

      createApp({
        data() {
          return {};
        },
        methods: {
          fn() {
            // 原生获取dom元素的方式
            // console.log(document.querySelector("h2"));

            console.log(this.$refs.h2dom);
          },
          fn2() {
            this.$refs.child.log();
          },
        },
        // created() {
        //   console.log(this.$refs.h2dom);
        // },
        components: {
          Child,
        },
      }).mount("#app");
    </script>
  </body>
</html>
